package com.rwy.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@XmlRootElement(name = "hash")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder()
public class PageInfo<T> implements Serializable {
	
	private static final long serialVersionUID = 4271378655516251360L;

	private int page = 1;

	private int total;

	private int pageSize = 20;

	private int maxPage;
	
	private int fromPage;
	
	private int toPage;
	
	private List<T> items;
	
	@SuppressWarnings("rawtypes")
	public  static PageInfo getInstance() {
		
		return new PageInfo<>();
	}
	
	public PageInfo(int page, int total, int pageSize, int maxPage, List<T> items) {
		super();
		this.page = page;
		this.total = total;
		this.pageSize = pageSize;
		this.maxPage = maxPage;
		this.fromPage = 0;
		this.toPage = 0;
		this.items = items;
	}
	public PageInfo(List<T> items) {
		super();
		this.fromPage = 0;
		this.toPage = 0;
		this.items = items;
	}
	
	public List<T> getItems() {
		if (items == null) this.items = new ArrayList<>();
		return this.items;
	}
	public  PageInfo<T> assemPaInfo(int page, int count, int pageSize, List<T> data) {
		this.maxPage = count % pageSize == 0 ? count / pageSize : (count / pageSize + 1);
		this.page = page;
		this.total = count;
		this.pageSize = pageSize;
		this.items = data;
		return this;
	}

}
